<?php

/**
 * Description of Administrator
 *
 * @author cjc
 */
class Administrator extends I_Private {

    function __construct() {
        parent::__construct();


        if (ENVIRONMENT == 'development') {
            //  $this->add_css('administrator/jasmine');
            //   $this->add_js('libs/angular-moncks');
            //  $this->add_js('tests/lib/jasmine-2.0.0/jasmine');
            //  $this->add_js('tests/lib/jasmine-2.0.0/jasmine-html');
            //$this->add_js('tests/lib/jasmine-2.0.0/boot');
            //$this->add_js('scripts/administrator/specAdmin');
        }


        //$this->data['url_admin'] = site_url(URL_ADMIN);
        $groups_exceptions = array('admin', 'development');
        $this->load->library('User_Manager');


        if (exceptions_uri(array(URL_ADMIN . '/login', URL_ADMIN . '/authentication')) == false) {
            if (userData()->id == 0) {
                redirect(URL_ADMIN . '/login');
            }
            if (userData()->id > 0 && !in_array(userData()->group, $groups_exceptions)) {
                redirect('/');
            }
        }
    }

    function index() {
        $this->data['login'] = userData()->login;
        $profile = $this->user_manager->get_users(userData()->id);
        $this->data['name'] = $profile->NOME;
        $this->data['profissao'] = $profile->PROFISSAO;
        $this->data['user_id'] = userData()->id;

        $this->add_css('administrator/bootstrap.min');
        $this->add_css('administrator/ionicons.min');
        $this->add_css('administrator/morris/morris');
        //$this -> add_css('administrator/trNgGrid');
        $this->add_css('administrator/jvectormap/jquery-jvectormap-1.2.2');
        $this->add_css('administrator/toast');
        $this->add_css('administrator/fullcalendar/fullcalendar');
        $this->add_css('administrator/daterangepicker/daterangepicker-bs3');
        $this->add_css('administrator/bootstrap-wysihtml5/bootstrap3-wysihtml5.min');
        $this->add_css('administrator/font-awesome-4.1/css/font-awesome.min');

        $this->add_css('administrator/iCheck/all');
        $this->add_css('administrator/datatables/dataTables.bootstrap');
        $this->add_css('administrator/administrator');

        $this->add_js('libs/jquery');
        $this->add_js('libs/angular.min');
        $this->add_js('libs/helpers');
        $this->add_js('plugins/administrator/i18n/angular-locale_pt-br');
        $this->add_js('plugins/administrator/jquery-ui-1.10.3.min');
        $this->add_js('plugins/administrator/bootstrap.min');
        //$this -> add_js('plugins/administrator/trNgGrid');
        $this->add_js('plugins/toast');
        $this->add_js('plugins/administrator/shortcut');
        $this->add_js('plugins/administrator/ngCocFilter', 'footer');
        $this->add_js('plugins/administrator/jquery.mask-ui', 'footer');

        $this->add_js('plugins/administrator/morris/morris.min', 'footer');
        $this->add_js('plugins/administrator/sparkline/jquery.sparkline.min', 'footer');
        $this->add_js('plugins/administrator/jvectormap/jquery-jvectormap-1.2.2.min', 'footer');
        $this->add_js('plugins/administrator/jvectormap/jquery-jvectormap-world-mill-en', 'footer');
        $this->add_js('plugins/administrator/fullcalendar/fullcalendar.min', 'footer');
        $this->add_js('plugins/administrator/jqueryKnob/jquery.knob', 'footer');
        $this->add_js('plugins/administrator/daterangepicker/daterangepicker', 'footer');
        $this->add_js('plugins/administrator/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js', 'footer');
        $this->add_js('plugins/administrator/ckeditor/ckeditor', 'footer');
        $this->add_js('plugins/administrator/iCheck/icheck.min', 'footer');
        $this->add_js('plugins/administrator/ngRoute/ngRoute', 'footer');
        $this->add_js('plugins/administrator/ngResource/ngResource', 'footer');
        //ngCocStorage
        $this->add_js('plugins/administrator/ngCocStorage', 'footer');
        $this->add_js('plugins/administrator/ui-bootstrap-tpls-0.10.0.min', 'footer');
        $this->add_js('plugins/administrator/ngAnimate/ngAnimate', 'footer');
        $this->add_js('plugins/administrator/upload-button-btn', 'footer');
        $this->add_js('plugins/administrator/jquery.mask', 'footer');
        $this->add_js('plugins/administrator/angularjs-mask', 'footer');
        $this->add_js('plugins/administrator/jquery.maskMoney', 'footer');
        $this->add_js('scripts/administrator/administratorConfigApplication');

        $this->add_js('scripts/administrator/ng-controllers');
        $this->add_js('scripts/administrator/app', 'footer');
        $this->add_js('scripts/administrator/main', 'footer');
        $this->add_js('scripts/administrator/ng-services', 'footer');
        $this->add_js('scripts/administrator/ng-directives', 'footer');
        $this->add_js('scripts/administrator/ng-factorys', 'footer');
        $this->ngController('leiloes');
        $this->ngController('lotes');
        $this->ngController('users');
        $this->ngController('banners');
        $this->ngController('leiloes');
        $this->ngController('leiloeiros');
        $this->ngController('arrematantes');
        $this->data['title'] = 'Home';
        $this->display('index');
    }

    function newLeilao() {
        $this->data['title'] = 'Cadastro de Leil�o';
        $this->display('leilao/form');
    }

    /**
     * Views 
     * metodo responsavel pela recuperação de paginas quando usamos 
     * ajax.
     * MVC angular usado.
     * @param string $views pagina php recuperada acessar subpastas a view deve ser passa pasta.view
     * 
     */
    function views($views) {
        $_views = preg_replace('#\.#', '/', $views);
        $this->view('views/' . $_views);
    }

    function SpecHtml() {

        $this->load->view('specs/administrator.php');
    }

    function comitentesUploadMultiple() {
        $this->load->library('csvreader');
        $file = array_values($_FILES);
        $file = $file['0'];
        $apply = array('CNPJ' => 'clear_number', 'TELEFONE_RECADO' => 'clear_number', 'TELEFONE' => 'clear_number', 'CEP' => 'clear_number', 'CELULAR' => 'clear_number', 'RAZAO' => 'trim');
        $data['response'] = $this->csvreader->parse_to_db($file['tmp_name'], array('strtoupper'), $apply);
        $data['asTotal'] = count($data['response']);
        header('content-type:text/json;');
        echo json_encode($data);
    }

    function login() {
       redirect(BASE_URL_USER . '/login');
    }

    function authentication() {

        $auth = $this->user_manager->auth($this->input->post('login'), $this->input->post('pwd'));
        if ($auth) {
            redirect($auth->page);
            return;
        }

        redirect(BASE_URL_USER . '/login');
    }
    
    

    function logout() {
        $this->user_manager->logout();
        redirect(BASE_URL_USER . '/login');
    }

    function ngController($name) {
        $file = 'app/controllers/' . $name . 'Controller';
        $this->add_js($file, 'footer');
    }

}
